package leetcode.DFS_BFS;

/**
 * @author mazouri
 * @create 2021-09-22 22:33
 */
public class _130_solve {
    public void solve(char[][] board) {
        int row = board.length;
        int col = board[0].length;

        for (int i = 0; i < row; i++) {
            if (board[i][0] == 'O') dfs(board, i, 0);
            if (board[i][col - 1] == 'O') dfs(board, i, col - 1);
        }

        for (int i = 0; i < col; i++) {
            if (board[0][i] == 'O') dfs(board, 0, i);
            if (board[row - 1][i] == 'O') dfs(board, row - 1, i);
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (board[i][j] == 'O') board[i][j] = 'X';
                if (board[i][j] == '#') board[i][j] = 'O';
            }
        }
    }

    private void dfs(char[][] board, int x, int y) {
        if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return;
        if (board[x][y] != 'O') return;
        board[x][y] = '#';
        dfs(board, x, y + 1);
        dfs(board, x + 1, y);
        dfs(board, x, y - 1);
        dfs(board, x - 1, y);
    }
}
